package com.apes.framework.plugin.soe.metadata;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：凭证接口
 *
 * @author xul
 * @create 2018-09-27 16:51
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BusinessAction.CONST_TABLE_NAME, indexes = {
        @Index(name = "inx_soe_business_type", columnList = "business_type_id")})
public class BusinessAction extends BaseModel {
    public static final String CONST_TABLE_NAME = "soe_business_action";

    @Id
    @Column(name = "id", length = 64)
    private String id;   //EsbId

    @Column(name = "name", nullable = false, length = 120)
    private String name; //描述

    @ManyToOne
    @JoinColumn(name = "business_type_id", nullable = false, referencedColumnName = "id")
    private BusinessType businessType; //凭证类型

    @Column(name = "type", nullable = false, length = 32)
    private String type; // 类型 inbound:入站;outbound:出站;mixing:出入站

    public void setType(String value) {
        if (!"inbound".equals(value) && !"outbound".equals(value) && !"mixing".equals(value)) {
            throw new RuntimeException("类型编码不对，请检查！");
        }
        type = value;
    }

    public boolean equals(BusinessAction that) {
        if (this == that) return true;
        if (!(that instanceof BusinessAction)) return false;

        if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false;
        if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) return false;
        if (getBusinessType() != null ? !getBusinessType().getId().equals(that.getBusinessType().getId()) : that.getBusinessType() != null)
            return false;
        return getType() != null ? getType().equals(that.getType()) : that.getType() == null;
    }

}
